﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Follow.Utils;
using Follow.Model;

namespace Follow.DataAccess
{
    public class TransacaoDAO
    {
        private SqlDataReader reader = null;
        private SqlConnection conn = null;
        private SqlCommand cmd = null;

        public TransacaoDAO()
        {
            conn = ConnectionUtils.getConnection();
        }

        public IList<Transacao> findAll(DateTime inicio, DateTime fim)
        {
            IList<Transacao> transacoes = new List<Transacao>();
            try
            {
                conn.Open();

                String sqlCommand = " select t.*, u.login from transacao t, usuario u where t.usuario =" +
                                    " u.idusuario and datatransacao >= @inicio and datatransacao <= @fim" +
                                    " order by datatransacao desc;";
                cmd = new SqlCommand(sqlCommand, conn);
                cmd.Parameters.Add(new SqlParameter("inicio", inicio));
                cmd.Parameters.Add(new SqlParameter("fim", fim));
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Transacao t = new Transacao();
                    t.IDTransacao = reader.GetInt32(0);
                    t.IDUsuario = reader.GetInt32(1);
                    t.Funcionalidade = reader.GetString(2);
                    t.Acao = reader.GetString(3);
                    t.Comando = reader.GetString(4);
                    t.Data = reader.GetDateTime(5);
                    t.Usuario = reader.GetString(6);
                    transacoes.Add(t);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (conn != null)
                {
                    conn.Close();
                }
            }
            return transacoes;
        }

        public Transacao findById(Int32 id)
        {
            Transacao t = new Transacao();
            try
            {
                conn.Open();
                String sqlCommand = " select t.*, u.login from transacao t, usuario u" +
                                    " where t.idtransacao = @id;";
                cmd = new SqlCommand(sqlCommand, conn);
                cmd.Parameters.Add(new SqlParameter("id", id));
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    t.IDTransacao = reader.GetInt32(0);
                    t.IDUsuario = reader.GetInt32(1);
                    t.Funcionalidade = reader.GetString(2);
                    t.Acao = reader.GetString(3);
                    t.Comando = reader.GetString(4);
                    t.Data = reader.GetDateTime(5);
                    t.Usuario = reader.GetString(6);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (conn != null)
                {
                    conn.Close();
                }
            }
            return t;
        }
    }
}